﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Derbster.Domain;
using Derbster.Peer.Commands;

namespace Derbster.Peer.Controllers
{
    [CommandGroup("Lineup")]
    public class LineupController
    {
        private int _matchLocalKey;
        private bool _useFirstTeam;
        private IMatchRepository _repository;
        public LineupController(IMatchRepository repository, int matchLocalKey, bool useFirstTeam)
        {
            Contract.Requires(repository != null);
            Contract.Requires(matchLocalKey != 0);

            _repository = repository;
            _matchLocalKey = matchLocalKey;
            _useFirstTeam = useFirstTeam;
        }

        [Command("Add-Jammer")]
        public void AddJammer(int playerId, int period, int jamNumber)
        {
            var match = _repository.FindById(_matchLocalKey);
            var jam = match.Jams.Single(j => j.Period == period && j.Number == jamNumber);
            if (_useFirstTeam)
            {
                var player = match.FirstTeamRoster.Single(p => p.PlayerId == playerId);
                jam.AddFirstTeamJammer(player);
            }
            else
            {
                var player = match.SecondTeamRoster.Single(p => p.PlayerId == playerId);
                jam.AddSecondTeamJammer(player);
            }
        }

        [Command("Add-Pivot")]
        public void AddPivot(int playerId, int period, int jamNumber)
        {
            var match = _repository.FindById(_matchLocalKey);
            var jam = match.Jams.Single(j => j.Period == period && j.Number == jamNumber);
            if (_useFirstTeam)
            {
                var player = match.FirstTeamRoster.Single(p => p.PlayerId == playerId);
                jam.AddFirstTeamPivot(player);
            }
            else
            {
                var player = match.SecondTeamRoster.Single(p => p.PlayerId == playerId);
                jam.AddSecondTeamPivot(player);
            }
        }

        [Command("Add-Blocker")]
        public void AddBlocker(int playerId, int period, int jamNumber)
        {
            var match = _repository.FindById(_matchLocalKey);
            var jam = match.Jams.Single(j => j.Period == period && j.Number == jamNumber);
            if (_useFirstTeam)
            {
                var player = match.FirstTeamRoster.Single(p => p.PlayerId == playerId);
                jam.AddFirstTeamBlocker(player);
            }
            else
            {
                var player = match.SecondTeamRoster.Single(p => p.PlayerId == playerId);
                jam.AddSecondTeamBlocker(player);
            }
        }

        [Command("Remove-Player")]
        public void RemovePlayer(int playerId, int period, int jamNumber)
        {
            var match = _repository.FindById(_matchLocalKey);
            var jam = match.Jams.Single(j => j.Period == period && j.Number == jamNumber);
            if (_useFirstTeam)
            {
                var player = match.FirstTeamRoster.Single(p => p.PlayerId == playerId);
                jam.RemoveFromFirstTeamLineup(player);
            }
            else
            {
                var player = match.SecondTeamRoster.Single(p => p.PlayerId == playerId);
                jam.RemoveFromSecondTeamLineup(player);
            }
        }

        [Command("Correct-Player")]
        public void CorrectPlayer(int playerId, int newPlayerId, int period, int jamNumber)
        {
            var match = _repository.FindById(_matchLocalKey);
            var jam = match.Jams.Single(j => j.Period == period && j.Number == jamNumber);
            if (_useFirstTeam)
            {
                var player = match.FirstTeamRoster.Single(p => p.PlayerId == playerId);
                var newPlayer = match.FirstTeamRoster.Single(p => p.PlayerId == newPlayerId);
                jam.CorrectFirstTeamLineup(player, newPlayer);
            }
            else
            {
                var player = match.SecondTeamRoster.Single(p => p.PlayerId == playerId);
                var newPlayer = match.SecondTeamRoster.Single(p => p.PlayerId == newPlayerId);
                jam.CorrectSecondTeamLineup(player, newPlayer);
            }
        }

        [Command("Injured-Player")]
        public void InjuredPlayer(int playerId, int period, int jamNumber)
        {
            var match = _repository.FindById(_matchLocalKey);
            var jam = match.Jams.Single(j => j.Period == period && j.Number == jamNumber);
            if (_useFirstTeam)
            {
                var player = match.FirstTeamRoster.Single(p => p.PlayerId == playerId); 
            }
            else
            {
                var player = match.SecondTeamRoster.Single(p => p.PlayerId == playerId);
            }
        }
    }
}
